#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

template<typename Iterator, typename dataType = typename std::iterator_traits<Iterator>::value_type>
size_t maxLIS(Iterator begin, Iterator end, Iterator outBegin)
{
    dataType maxV = *begin;
    dataType minV = *begin;
    for (Iterator i = begin; i != end; ++i)
    {
        if (*i < minV)
            minV = *i;
        if (maxV < *i)
            maxV = *i;
    }
    std::vector<dataType> maxf(end - begin + 1);
    std::vector<int> prev(end - begin);
    maxf[0] = minV - 1;
    for (size_t i = 1; i < maxf.size(); ++i)
        maxf[i] = maxV + 1;
    for (size_t i = 1; i < prev.size(); ++i)
        prev[i] = -1;

    for (Iterator i = begin; i != end; ++i)
    {
        size_t j = upper_bound (maxf.begin(), maxf.end(), *i) - maxf.begin();
        if ((maxf[j-1] < *i) && (*i < maxf[j]))
        {
            maxf[j] = *i;
            *(prev.begin() + (i - begin)) = j + 1;
        }
    }

    size_t maxLen = 0;
    for (size_t i = 1; i < maxf.size(); ++i)
        if (maxf[i] >= minV && maxf[i] <= maxV)
            maxLen = i;

    int prevVal = maxLen;
    size_t i = 0;
    while(i < maxLen)
    {
        *(outBegin + i) = maxf[prevVal];

        prevVal = prev[prevVal];
        ++i;
    }

    return maxLen;
}

int main()
{
    std::vector<int> data;
    std::vector<int> lis;
    size_t n;
    std::cin >> n;
    data.resize(n);
    lis.resize(n);
    for (size_t i = 0; i < n; ++i)
        std::cin >> data[i];
    size_t maxLen = maxLIS(data.begin(), data.end(), lis.begin());
    lis.resize(maxLen);
    std::cout << maxLen << "\n";
    for (size_t i = 0; i < maxLen; ++i)
        std::cout << lis[i] << " ";
    std::cout << "\n";
    return 0;
}
